package com.zjl.hiring.talent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.zjl.hiring.common.Constant;
import com.zjl.hiring.common.util.AppUtil;
import com.zjl.hiring.company.dao.ApplicationDao;
import com.zjl.hiring.company.entity.ApplicationEntity;
import com.zjl.hiring.talent.dao.ResumeDao;
import com.zjl.hiring.talent.dao.TalentDao;
import com.zjl.hiring.talent.entity.ResumeEntity;
import com.zjl.hiring.talent.entity.TalentEntity;

public class TalentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TalentDao talentDao = new TalentDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("register".equals(command)) {
			register(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}

	}

	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 登录成功查询当前用户的所得简历
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);

		// 查询当前职位的全部信息
		List<ApplicationEntity> applicationList = new ArrayList<>();
		applicationList = applicationDao.listAllapplication(talentId);
		request.setAttribute("applicationList", applicationList);

		String result = request.getParameter("result");
		// 登陆成功不传递
		if (result != null) {
			request.setAttribute("result", result);
		}

		request.getRequestDispatcher("view/talent/main.jsp").forward(request,
				response);
	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String code = request.getParameter("code");
		String password = request.getParameter("password");

		TalentEntity talent = talentDao.login(code, password);
		if (talent == null) {
			response.sendRedirect("view/talent/login.jsp?loginError=true");
		} else {
			request.getSession().setAttribute("talent", talent);
			// 登录成功查询当前用户的所得简历
			// List<ResumeEntity> resumeList = ResumeDao.list(talent.getId());
			// request.setAttribute("resumeList", resumeList);
			//
			// request.getRequestDispatcher("view/talent/main.jsp").forward(
			// request, response);
			response.sendRedirect("TalentServlet?command=main");
		}

	}

	/**
	 * 注册
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1.获取参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbies = request.getParameterValues("hobby");
		TalentEntity talent = new TalentEntity();
		talent.setCode(code);
		talent.setPassword(password);
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		StringBuilder sb = new StringBuilder();
		// FIXME 爱好？？？
		if (hobbies != null) {
			sb.append(StringUtils.join(hobbies, "|"));
		}
		talent.setHobby(sb.toString());
		// 2、调用业务逻辑；

		int row = talentDao.register(talent);
		// 3、转向
		if (row == 1) {
			response.sendRedirect("view/talent/login.jsp?registerSuccess=true");
		} else {
			request.setAttribute("talent", talent);
			request.getRequestDispatcher("view/talent/register.jsp").forward(
					request, response);
		}
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();
		response.sendRedirect("view/talent/login.jsp?sessionInvalidate=true");
	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		// 1.获取参数
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");

		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbies = request.getParameterValues("hobby");

		TalentEntity talent = new TalentEntity();
		talent.setId(talentEntity.getId());
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		// FIXME 爱好？？？
		if (hobbies != null) {
			talent.setHobby(StringUtils.join(hobbies, "|"));
		} else {
			talent.setHobby("");
		}
		// 2、调用业务逻辑；

		int row = talentDao.update(talent);
		// 3、转向
		if (row == 1) {
			// 更新session
			talent.setCode(talentEntity.getCode());
			request.getSession().setAttribute("talent", talent);
			response.sendRedirect("view/talent/talent-info.jsp?result="
					+ Constant.OPERATE_SUCCESS);
		} else {
			response.sendRedirect("view/talent/talent-info.jsp?result="
					+ Constant.OPERATE_ERROR);
		}
	}

}
